package com.zsj.juc.Atomic;

import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;

/**
 * @author zsj
 * @version 1.0
 * @date 2024/5/17 15:38
 * @description longAdder
 */
public class LongAdderDemo {

    public static void main(String[] args) {

        LongAdder longAdder = new LongAdder();//初始化一个为0的累加器

        longAdder.increment();
        longAdder.increment();
        longAdder.increment();
        longAdder.increment();
        //累加4次
        System.out.println(longAdder.sum());

        /*
         public static long sum(long a, long b) {
            return a + b;
         }
         */
        LongAccumulator longAccumulator = new LongAccumulator(Long::sum, 0);
        longAccumulator.accumulate(1);
        longAccumulator.accumulate(3);

        System.out.println(longAccumulator.get());

    }
}
